package com.wibo.account;

import weibo4android.User;
import weibo4android.Weibo;
import weibo4android.WeiboException;

import com.wibo.common.UserItem;
import com.wibo.common.Utility;

import android.content.Context;
import android.content.res.Resources.Theme;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class UserListItemView extends LinearLayout {
	private Context mContext;
	private static Weibo mWeibo = null;
	private static OAuthConstant mAuthConstant = null;
	CUserManager mUserManager = null;
	
	private ImageView mUserView;
	private TextView mUserName;
	private TextView mFocusCount;
	private TextView mFansCount;
	private TextView mWeiboCount;
	private ImageView mVLogo;
	
	private RelativeLayout mFocusLayout;
	private TextView mOpeateText;
	
	private Thread mThread = null;
	public UserListItemView(Context context) {
		super(context);
		
		mUserManager = CUserManager.getInstance(context);
		if (mAuthConstant == null)
			mAuthConstant = OAuthConstant.getInstance();

		if (mWeibo == null)
			mWeibo = mAuthConstant.getWeibo();
		
		initialize(context);
	}
	
	private int getListItemSkin() {
		int skin = mUserManager.getMySkin();
		switch (skin) {
		case 0:
			return R.layout.userlistitem;
		case 1:
			return R.layout.userlistitem_2;
		default:
			return R.layout.userlistitem;
		}
	}

	private void initialize(Context context) {
    	this.mContext = context;
    	
    	View view = LayoutInflater.from(this.mContext).inflate(getListItemSkin(), null);
    	
    	mUserView = (ImageView)view.findViewById(R.id.userHeadView);
    	mUserName = (TextView) view.findViewById(R.id.userName);
    	mFocusCount = (TextView) view.findViewById(R.id.focusCount);
    	mFansCount = (TextView) view.findViewById(R.id.fansCount);
    	mWeiboCount = (TextView) view.findViewById(R.id.weibosCount);
    	mVLogo = (ImageView) view.findViewById(R.id.vLogo);
    	mVLogo.setVisibility(ImageView.GONE);
    	
    	mFocusLayout = (RelativeLayout) view.findViewById(R.id.userOperate);
    	mOpeateText = (TextView) view.findViewById(R.id.operateText);
    	
    	addView(view);
	}
	
	private static final int CREATE_FRIEND_FINISHED = 0;
	private static final int CREATE_FRIEND_ERROR = 1;
	private static final int DESTROY_FRIEND_FINISHED = 2;
	private static final int DESTROY_FRIEND_ERROR = 3;
	
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case CREATE_FRIEND_FINISHED:
				if (msg.obj != null) {
					User user = (User)msg.obj;
					mFocusLayout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.upload_progress_bar_orange));
					mOpeateText.setText(mContext.getString(R.string.deletefocus));
					mUserManager.getMyFocusIds().addId(user.getId());
				}
				
				break;
			case CREATE_FRIEND_ERROR:
				Toast.makeText(mContext, mContext.getString(R.string.addfocusFailed), Toast.LENGTH_SHORT).show();
				break;
			case DESTROY_FRIEND_FINISHED:
				if (msg.obj != null) {
					User user = (User)msg.obj;
					mFocusLayout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.upload_progress_bar));
					mOpeateText.setText(mContext.getString(R.string.addfocus));
					mUserManager.getMyFocusIds().deleteId(user.getId());
				}
				break;
			case DESTROY_FRIEND_ERROR:
				Toast.makeText(mContext, mContext.getString(R.string.destroyfocusFailed), Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};
	
	private void sendMessage(int what, int arg1, int arg2, Object obj) {
		Message msg = mHandler.obtainMessage(what, arg1, arg2, obj);
		mHandler.sendMessage(msg);
	}
	
	public void updateView(final UserItem userItem, String position) {
		if (userItem.getmWeiboUserPic() != null) {
			mUserView.setImageBitmap(Utility.getBitmap(mContext, userItem.getmWeiboUserPic(), Utility.WEIBO_PIC, Utility.USER_PIC));
		}
		
		mUserName.setText(userItem.getmUserName());
		mFocusCount.setText(String.valueOf(userItem.getmFriendsCount()));
		mFansCount.setText(String.valueOf(userItem.getmFollowersCount()));
		mWeiboCount.setText(String.valueOf(userItem.getmStatusesCount()));
		
		if (userItem.ismVLogo()) {
			mVLogo.setVisibility(ImageView.VISIBLE);
		}else{
			mVLogo.setVisibility(ImageView.GONE);
		}
		
		if (userItem.ismIsFocus()) {
			mFocusLayout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.upload_progress_bar_orange));
			mOpeateText.setText(mContext.getString(R.string.deletefocus));
		}else{
			mFocusLayout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.upload_progress_bar));
			mOpeateText.setText(mContext.getString(R.string.addfocus));
		}
		
		mFocusLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mThread != null) {
					if (mThread.isAlive()) {
						return;
					}
				}
				
				mThread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						try {
							if (userItem.ismIsFocus()) {
								User user = mWeibo.destroyFriendship(String.valueOf(userItem.getmUserId()));
								
								if (user != null) {
									userItem.setmIsFocus(false);
									sendMessage(DESTROY_FRIEND_FINISHED, 0, 0, user);
								}else{
									sendMessage(DESTROY_FRIEND_ERROR, 0, 0, null);
								}
							}else{
								User user = mWeibo.createFriendship(String.valueOf(userItem.getmUserId()));
								
								if (user != null) {
									userItem.setmIsFocus(true);
									sendMessage(CREATE_FRIEND_FINISHED, 0, 0, user);
								}else{
									sendMessage(CREATE_FRIEND_ERROR, 0, 0, null);
								}
							}
						}catch (WeiboException e) {
							e.printStackTrace();
							if (userItem.ismIsFocus()) {
								sendMessage(DESTROY_FRIEND_ERROR, 0, 0, null);
							}else{
								sendMessage(CREATE_FRIEND_ERROR, 0, 0, null);
							}
						}
						
					}
				});
				
				mThread.start();
			}
		});
	}
}
